package com.isoft.c2team3service2.config;

import com.isoft.c2team3service2.mapper.ArtistMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.time.LocalDateTime;
import java.util.Map;

import static com.isoft.c2team3service2.util.RedisConstants.ARTIST_RECENT_ACCESS_TIME;
import static com.isoft.c2team3service2.util.RedisConstants.USER_LOGIN_KEY;

/**
 * @author 蓝莓
 * @date 2025/9/10 14:16
 */
@Configuration
@EnableAspectJAutoProxy
@Aspect
public class AOPAspectConfig {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ArtistMapper artistMapper;

    @After("execution(* com.isoft.c2team3service2.controller.PostController.searchPostsByArtist(..))")
    public void afterArtistController() {
        try {
            // 从请求头或Cookie中获取token
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if (requestAttributes != null) {
                HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
                String token = request.getHeader("authorization");

                if (token != null && token.startsWith("Bearer ")) {
                    token = token.substring(7);
                }

                if (token != null) {
                    String userLoginKey =USER_LOGIN_KEY + token;
                    Boolean hasKey = stringRedisTemplate.hasKey(userLoginKey);
                    System.out.println("Key exists: " + hasKey);

                    if (hasKey) {
                        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(userLoginKey);
                        System.out.println("entries: " + entries);

                        Object artistId = entries.get("id");
                        if (artistId != null) {
                            Integer id;
                            if (artistId instanceof String) {
                                id = Integer.valueOf((String) artistId);
                            } else if (artistId instanceof Integer) {
                                id = (Integer) artistId;
                            } else {
                                id = Integer.valueOf(artistId.toString());
                            }
                            String s = artistMapper.selectIs(id);
                            if (s != null) {
                                stringRedisTemplate.opsForHash().put(ARTIST_RECENT_ACCESS_TIME, s, LocalDateTime.now().toString());
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
    }
}
}
